'use strict';

const { lt } = require('semver');
const { red } = require('colors/safe');
const log = require('@ysx-cli/log');
const { LOWEST_NODE_VERSION } = require('@ysx-cli/utils');
class Command {
  constructor(argv) {
    if (!argv || !Array.isArray(argv)) {
      throw new Error('请传入正确的Command参数');
    }
    this.argv = argv;
    const runner = new Promise(() => {
      let chain = Promise.resolve();
      chain = chain.then(() => this.checkNodeVersion());
      chain = chain.then(() => this.initArgs());
      // 执行子类方法
      chain = chain.then(() => this.init());
      chain = chain.then(() => this.exec());
      chain.catch(error => log.error(error.message));
    });
  }
  init() {
    throw new Error('init 必须实现');
  }
  exec() {
    throw new Error('exec 必须实现');
  }
  initArgs() {

  }
  checkNodeVersion() {
    const currentVersion = process.version;
    if (lt(currentVersion, LOWEST_NODE_VERSION)) {
      throw new Error(red('Node 版本不能低于' + LOWEST_NODE_VERSION));
    }
  }
}
module.exports = Command;
